package com.gitee.wsl.struct.grammar.ast.process.constraints.ext

import com.gitee.wsl.text.reader.line.LookaheadText
import com.gitee.wsl.struct.grammar.ast.process.constraints.TextReadConstraints

fun TextReadConstraints.getNextLineFromConstraints(pos: LookaheadText.Position): CharSequence? {
    val line = pos.nextLineText ?: return null
    val nextLineConstraints = applyToNextLine(pos.nextLinePosition())
    return if (nextLineConstraints.extendsPrev(this)) {
        nextLineConstraints.eatItselfFromString(line)
    } else {
        null
    }
}

fun TextReadConstraints.eatItselfFromString(s: CharSequence): CharSequence {
    return if (s.length < charsEaten) {
        ""
    } else {
        s.subSequence(charsEaten, s.length)
    }
}